#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2014年10月16日

@author: chenyongbing
'''
import ConfigParser,os,re
import base64
import sys
current_dir = os.path.split(os.path.realpath(__file__))[0]
import logging


# config_file = os.path.join(os.path.dirname(__file__),'../conf/logging.conf')
# os.chdir(os.path.dirname(config_file))
# logging.basicConfig()
# logging.config.fileConfig(config_file)
# logger = logging.getLogger('base')

class LocalConfig(object):
    '''
    classdocs
    '''


    def __init__(self, config_file=None):
        self.config = ConfigParser.SafeConfigParser()
        if config_file == None:
            config_file = os.path.join(current_dir,'../conf/config.ini')
        self.config.read(config_file)

        self.log_config_file = os.path.join(current_dir,self.config.get('log','config_file'))

        self.mysqlConfig = self.getConfigByRegToDict( para='mysql')
        
        self.get_export_rule()
        
        self.emailConfig = self.getConfigByFullName( section='email')
        
        self.hiveConfig = self.getConfigByFullName( section='hive')
    
    
    def analyze_value(self,option,value):
        if option=='maxbytes':
            value = eval(value)
        
        elif option=='passwd':
            value = self.analysis_passwd(value)
        elif re.search('_path|_file',option):
            value = os.path.join(current_dir,value)
        elif re.search('_list',option):
            value = value.split(',')
        elif re.search('_dict',option):
            value = eval(value.strip())
        elif re.search('count|port',option):
            value = int(value)
        return value
    
    def getConfigByFullName(self,config=None,section=''):
        if config==None:
            config = self.config
        msg = {}
        for option in config.options(section):
            
            value = config.get(section,option)
            value = self.analyze_value(option, value)
            msg[option] = value 
        return msg
        
    def show_passwd(self,s,f):
        passwd = self.config.get(s,f)
        new_passwd = base64.b64decode(passwd)
        return new_passwd
    
    def getConfigByRegToDict(self,config=None,para=''):
        if config==None:
            config = self.config
        msgs = {}
        sections = config.sections()
        for section in sections:
            if not re.search(para,section):
                continue
            name = re.sub('%s\.*'%para,'',section)
            msg = {}
            for option in config.options(section):
                value = config.get(section,option)
                value = self.analyze_value(option, value)
                msg[option] = value

            msgs[name] = msg
        return msgs
        
    def analysis_passwd(self,passwd):
        #passwd_lens = len(passwd)
        #passwd_lenx = passwd_lens - (passwd_lens % 4 if passwd_lens % 4 else 4)
        #new_passwd = base64.b64decode(passwd[:passwd_lenx])
        new_passwd = base64.b64decode(passwd)
        return new_passwd

    def get_export_rule(self):
        config = ConfigParser.SafeConfigParser()
        config_file = os.path.join(current_dir,'../conf/export_rules.ini')
        if not os.path.exists(config_file):return None
        config.read(config_file)
        sections = self.config.sections()
        self.rules = self.getConfigByRegToDict(config=config, para='rule.')
baseLocalConfig = LocalConfig()
    
if __name__=="__main__":
   
    
    print baseLocalConfig.hiveConfig
    